let logger = require("pomelo-logger").getLogger(__filename);
let BaseTableService = require("../base/tableService");
let util = require('util');
let YuanBaoTableCreator = require("./zhuojiYuanBaoTable").create;
let PlayerCreator = require("./zhuojiPlayer").create;
let uuid = require('node-uuid');
let ZhuojiGameConstant = require("../../mahjong/constant/zhuojiGameConstant");

/*开放的快速匹配场个数*/
const QUICK_JOIN_ROOM_NUM = 4;
const SYSTEM_TABLE_NUM = 50;

let Service = function (app) {
    BaseTableService.call(this, app);
    this.gid = 10000;
};

util.inherits(Service, BaseTableService);

Service.prototype.getGid = function () {
    return this.gid;
};

Service.prototype.doCreateSystemTable = function () {
    let self = this;
    let rooms = this.app.get("rooms");
    for (let i = 0; i < QUICK_JOIN_ROOM_NUM && i < rooms.length; i++) {
        let room = rooms[i];
        for (let j = 0; j < SYSTEM_TABLE_NUM; j++) {
            self.doCreateYuanBaoTable({
                tid: uuid.v4(),
                code: "0",
                room: room,
                maxPlayers: 4,
                visible: 1,
                creatorUid: 0,
                gid: 10000
            });
        }
        logger.info(`zhuoji room rid=${room.rid} createSystemTable ${SYSTEM_TABLE_NUM}个`);
    }
};

Service.prototype.doCreatePlayer = function (playerData, frontendId, sessionId) {
    return PlayerCreator(playerData, frontendId, sessionId);
};

Service.prototype.doValidateCreateTableParams = function (params) {
    return {resultCode: 0, message: "ok"};
};

Service.prototype.doCreateYuanBaoTable  = function (data) {
    let table = YuanBaoTableCreator(data);
    this.tables.push(table);
    return table;
};

Service.prototype.doPlayerReconnect = function (table, player) {
    player.push("onInitializeZhuojiTable", {
        table: table.toJSON(),
        myCards: player.cardsInHand,
        cardGrab: player.cardGrab
    });
    if (table.currentPosition === player.getPosition()) {
        player.push("onZhuojiNotifyActive", {
            uid : player.getUid(),
            operation : table.getWaitingPlayerOperate().operation,
            tingResults : table.chu.uid === -1 ? table.getTing14Hint(player, player.cardGrab) : [],
            gangList : table.getWaitingPlayerOperate().gangList,
            grabCard : table.chu.uid === -1 ? player.getCardGrab() : 0,
        });
    }
};

Service.prototype.operate = function (frontendId, sessionId, operation, card, callback) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        callback(null, {resultCode: 404, message: "桌子不存在"});
        return;
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        callback(null, {resultCode: 404, message: "玩家不在线"});
        return;
    }
    if (!table.getIsPlaying()) {
        callback(null, {resultCode: 404, message: "游戏未开始"});
        return;
    }
    if (!operation) {
        callback(null, {resultCode: 404, message: "operation为空"});
        return;
    }
    card = card || 0;
    logger.info(`handler operate operation:0x${operation.toString(16)}, card:0x${card.toString(16)}`);
    if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_CHU
        || operation === ZhuojiGameConstant.MAHJONG_OPERTAION_PENG
        || operation === ZhuojiGameConstant.MAHJONG_OPERTAION_GANG
        || operation === ZhuojiGameConstant.MAHJONG_OPERTAION_TING
        || operation === ZhuojiGameConstant.MAHJONG_OPERTAION_HU
        || operation === ZhuojiGameConstant.MAHJONG_OPERTAION_CANCEL) {
        callback(null, {resultCode: 0, message: "ok"});
    }

    if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_CHU) {
        table.playerChuOperation(player, card);
    } else if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_PENG) {
        table.player_op_peng(player);
    } else if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_GANG) {
        table.player_op_gang(player, card);
    } else if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_TING) {
        table.player_op_ting(player);
    } else if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_HU) {
        table.player_op_hu(player);
    } else if (operation === ZhuojiGameConstant.MAHJONG_OPERTAION_CANCEL) {
        table.player_cancel_chi_peng_gang_hu(player);
    } else {
        callback(null, {resultCode: 0, message: "operation非法"});
    }
};

Service.prototype.tuoGuan = function (frontendId, sessionId, auto) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return {resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return {resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return {resultCode: 404, message: "游戏未开始"};
    }
    if (auto !== 0 && !auto) {
        return {resultCode: 404, message: "auto为空"};
    }
    if (auto === 1 || auto === 0) {
        table.tuoGuan(player, auto);
        return {resultCode: 0, message: "ok"};
    } else {
        return {resultCode: 0, message: "auto非法"};
    }
};


Service.prototype.tingBulb = function (frontendId, sessionId) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return {resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return {resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return {resultCode: 404, message: "游戏未开始"};
    }
    let results = table.getTing13Hint(player);
    player.push("onZhuojiTingBulb", {tingCardsNums: results});
    return {resultCode: 0, message: "ok"};
};

module.exports.create = function (app) {
    return new Service(app);
};